package org.niu.array;

import java.util.Arrays;

public class FindMinMoves {
    public int findMinMoves(int[] machines) {
        int n = machines.length-1;
        int all = 0;
        for(int i = 0;i < machines.length;i++){
            all += machines[i];
        }
        if(all % n != 0) return -1;
        int k = all / n;
        int[] result = new int[n];
        Arrays.fill(result,k);
        for (int i = 0; i < n; i++) {
            result[i] = machines[i]-result[i];
        }
        int max = 0;
        for (int i = 0; i < n-1; i++) {
            result[i] = result[i]-result[i+1];
            result[i+1] = result[i+1]*2;
            max = Math.max(result[i+1],max);
        }
        return max;
     }
}
